package cn.dubito.miaosha.controller;

import cn.dubito.miaosha.domain.User;
import cn.dubito.miaosha.rabbitmq.send.MqSender;
import cn.dubito.miaosha.redis.RedisService;
import cn.dubito.miaosha.redis.UserKey;
import cn.dubito.miaosha.service.UserService;
import cn.dubito.miaosha.utils.IdGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author liliang
 * @description:
 * @date 2018/3/28.
 */
@Controller
@RequestMapping("/demo")
public class SimpleController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private MqSender mqSender;

//    @RequestMapping("/head")
//    @ResponseBody
//    public Result<String> header() {
//        mqSender.sendHead("Hello mq");
//        return Result.success("ok");
//    }
//
//    @RequestMapping("/fanout")
//    @ResponseBody
//    public Result<String> fanout() {
//        mqSender.sendFanout("Hello mq");
//        return Result.success("ok");
//    }
//
//    @RequestMapping("/topic")
//    @ResponseBody
//    public Result<String> topic() {
//        mqSender.sendTopic("Hello mq");
//        return Result.success("ok");
//    }
//
//    @RequestMapping("/mqSend")
//    @ResponseBody
//    public Result<String> mqSend() {
//        mqSender.send("Hello mq");
//        return Result.success("ok");
//    }

    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model) {
        model.addAttribute("name","jack");
        return "hello";
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public User get(Integer id) {
        return userService.getById(id);
    }

    @RequestMapping("/db/add")
    @ResponseBody
    public String testTx() {
       userService.testTx();
        return "ssdfsdf";
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public User getRedis() {
        User user = redisService.get(UserKey.getById, "1", User.class);
        return user;
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public String setRedis() {
        User user = new User();
        user.setName("Redis");
        user.setId(6);
        redisService.set(UserKey.getById,"" + 1,user);
        return "ok";
    }

    @RequestMapping("/printId")
    @ResponseBody
    public Long printId() {
        long id = IdGeneratorUtil.getId();
        System.out.println(id);
        System.out.println();
        return id;
    }

}
